﻿// ***********************************************************************
// Assembly         : AccesoDatos
// Author           : Diego
// Created          : 03-02-2014
//
// Last Modified By : Diego
// Last Modified On : 03-02-2014
// ***********************************************************************
// <copyright file="AD_Orden.cs" company="">
//     Copyright (c) . All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using Modelado.Modulos.ClasesModelado;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// The ClasesAccesoDatos namespace.
/// </summary>
namespace AccesoDatos.Modulos.ClasesAccesoDatos
{
    /// <summary>
    /// Class AD_Orden.
    /// </summary>
    public class AD_Orden
    {

        //1 Crear Orden

        /// <summary>
        /// Crs the e_ orden.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="obj">The object.</param>
        /// <returns>System.Int32.</returns>
        public int CRE_Orden(SqlCommand comando, MOD_Orden obj)
        {
            try
            {
                string sp = "SP_CRE_Orden";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();


                comando.Parameters.Add(new SqlParameter("@CodigoProyecto", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@ord_descripcion", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@ord_codusuario", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@ord_totalordest", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@ord_totalordnoest", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@ord_impventas", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@ord_grantotal", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@ord_codestado", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@ord_fecha", SqlDbType.DateTime));

                comando.Parameters[0].Value = obj.ORD_codproyecto;
                comando.Parameters[1].Value = obj.ORD_descripcion;
                comando.Parameters[2].Value = obj.ORD_codusuario;
                comando.Parameters[3].Value = obj.ORD_totalordest;
                comando.Parameters[4].Value = obj.ORD_totalordnoest;
                comando.Parameters[5].Value = obj.ORD_impventas;
                comando.Parameters[6].Value = obj.ORD_grantotal;
                comando.Parameters[7].Value = obj.ORD_codestado;
                comando.Parameters[8].Value = obj.ORD_fecha;
                


                var returnParameter = comando.Parameters.Add("@return_value ", SqlDbType.Int);
                returnParameter.Direction = ParameterDirection.ReturnValue;

                comando.ExecuteNonQuery();



                int codigo = Convert.ToInt32(returnParameter.Value);
                return codigo;


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }



        //2eliminar Orden

        /// <summary>
        /// Des the l_ orden.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="codigo">The codigo.</param>
        public void DEL_Orden(SqlCommand comando, int codigo)
        {
            try
            {
                string sp = "SP_DEL_Orden";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();


                comando.Parameters.Add(new SqlParameter("@ord_codorden", SqlDbType.Int));

                comando.Parameters[0].Value = codigo;

                comando.ExecuteNonQuery();


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
        //3actualiza Orden

        /// <summary>
        /// Ups the d_ orden.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="obj">The object.</param>
        public void UPD_Orden(SqlCommand comando, MOD_Orden obj)
        {
            try
            {
                string sp = "SP_UPD_Orden";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@ord_codorden", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@CodigoProyecto", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@ord_descripcion", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@ord_codusuario", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@ord_totalordest", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@ord_totalordnoest", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@ord_impventas", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@ord_grantotal", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@ord_codestado", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@ord_fecha", SqlDbType.DateTime));

                comando.Parameters[0].Value = obj.ORD_codorden;
                comando.Parameters[1].Value = obj.ORD_codproyecto;
                comando.Parameters[2].Value = obj.ORD_descripcion;
                comando.Parameters[3].Value = obj.ORD_codusuario;
                comando.Parameters[4].Value = obj.ORD_totalordest;
                comando.Parameters[5].Value = obj.ORD_totalordnoest;
                comando.Parameters[6].Value = obj.ORD_impventas;
                comando.Parameters[7].Value = obj.ORD_grantotal;
                comando.Parameters[8].Value = obj.ORD_codestado;
                comando.Parameters[9].Value = obj.ORD_fecha;

                comando.ExecuteNonQuery();


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }


        /// <summary>
        /// Ups the d_ total_ orden.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="obj">The object.</param>
        public void UPD_Total_Orden(SqlCommand comando, MOD_Orden obj)
        {
            try
            {
                string sp = "SP_UPD_Total_Orden";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@ord_codorden", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@ord_totalordest", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@ord_totalordnoest", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@ord_impventas", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@ord_grantotal", SqlDbType.Decimal));
        
             

                comando.Parameters[0].Value = obj.ORD_codorden;
                comando.Parameters[1].Value = obj.ORD_totalordest;
                comando.Parameters[2].Value = obj.ORD_totalordnoest;
                comando.Parameters[3].Value = obj.ORD_impventas;
                comando.Parameters[4].Value = obj.ORD_grantotal;
         

                comando.ExecuteNonQuery();


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
        //trae info de Orden para editar
        /// <summary>
        /// Ses the l_ orden by identifier.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="codorden">The codorden.</param>
        /// <returns>MOD_Orden.</returns>
        public MOD_Orden SEL_OrdenByID(SqlCommand comando, int codorden)
        {
            try
            {
                string sp = "SP_SEL_OrdenByID";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@ord_codorden", SqlDbType.Int));


                comando.Parameters[0].Value = codorden;


                IDbDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                MOD_Orden obj = new MOD_Orden();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    obj.ORD_codproyecto = Convert.ToInt32(dt.Rows[i]["ORD_CODPROYECTO"]);
                    obj.ORD_descripcion = dt.Rows[i]["ORD_DESCRIPCION"].ToString();
                    obj.ORD_nombreusuario = dt.Rows[i]["USU_NOMBRECOMPLETO"].ToString();
                    obj.ORD_totalordest = Convert.ToDecimal(dt.Rows[i]["ORD_TOTALORDEST"]);
                    obj.ORD_totalordnoest = Convert.ToDecimal(dt.Rows[i]["ORD_TOTALORDNOEST"]);
                    obj.ORD_impventas =  Convert.ToDecimal(dt.Rows[i]["ORD_IMPVENTAS"]);
                    obj.ORD_grantotal = Convert.ToDecimal(dt.Rows[i]["ORD_GRANTOTAL"]);
                    obj.ORD_codestado = Convert.ToInt32(dt.Rows[i]["ORD_CODESTADO"]);
                    obj.ORD_fecha = Convert.ToDateTime(dt.Rows[i]["ORD_FECHA"]);

                }

                return obj;


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        ////4 muestra grid de ordenes Del proyecto en el cual se esta trabajando
        /// <summary>
        /// Ses the l_ grid_ orden.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="codProyeto">The cod proyeto.</param>
        /// <returns>List{MOD_Orden}.</returns>
        public List<MOD_Orden> SEL_Grid_Orden(SqlCommand comando, int codProyeto)
        {
            try
            {
                string sp = "SP_SEL_Grid_Orden";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@CodigoProyecto", SqlDbType.Int));


                comando.Parameters[0].Value = codProyeto;

                IDbDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                List<MOD_Orden> lista = new List<MOD_Orden>();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    MOD_Orden obj = new MOD_Orden();
                    obj.ORD_codorden = Convert.ToInt32(dt.Rows[i]["ORD_CODORDEN"]);
                    obj.ORD_fecha = Convert.ToDateTime(dt.Rows[i]["ORD_FECHA"]);
                    obj.ORD_nombreestado = (dt.Rows[i]["ESTORD_NOMBRE"]).ToString();

                    lista.Add(obj);
                }


                return lista;

            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        /// <summary>
        /// Ses the l_ ultimocodorden.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <returns>System.Int32.</returns>
        public int SEL_ULTIMOCODORDEN(SqlCommand comando)
        {
            try
            {
                string sp = "SP_SEL_CODORDEN";
                int cod_orden = 0;
                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

              

                IDbDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
       

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                  cod_orden = Convert.ToInt32(dt.Rows[i]["ORDCOD"]);


                }

                return cod_orden;


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }


    }
}
